Skip to content

Refactor name check and title update so works with custom prompt - #584

Merged
jeremypw merged 11 commits into
masterfrom
fix-same-name-regression
May 25, 2021
Merged

jeremypw merged 11 commits into
masterfrom
fix-same-name-regression

Conversation

@jeremypw

@jeremypw jeremypw commented May 24, 2021 •

Copy link
Copy Markdown
Collaborator

Fixes #583
Fixes #567

Replaces #568

  • Run update on new tab, switch tab, close tab and contents changed
  • Do not use terminal.window_title

Jeremy Wootten added 2 commits May 23, 2021 19:22
* Run update on new tab, switch tab, close tab and `Return` entered
* Do not use terminal.window_title
* In case a script changes the CWD
Comment thread src/MainWindow.vala Outdated
}

t.contents_changed.connect (() => {
schedule_name_check ();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets called "whenever the visible appearance of the terminal has changed". This means it is called on every keystroke and also when a program which outputs a log messages or graphical program like htop changes the contents of the terminal. So this means it is called approximately 50-500 times per minute for normal terminal usage. So it should be made sure that this calls are not too expensive.

At the moment this callback eventually calls save_opened_terminals, which is relatively expensive operation. For my machine the CPU usage more than doubled when using vim.

On counter measure would be to check if the terminal has a running foreground process and return immediately if this is the case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should also only be run on user input and not if the contents of the terminal are changed by a program.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Denouncing the contents_changed by ~500ms could probably also help.

@felix-andreas felix-andreas May 24, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another idea: Doesn't changing the directory always require to press Enter? Wouldn't be the simplest solution to schedule the name_check every time Enter is pressed and no foreground process is running?

@jeremypw jeremypw May 25, 2021 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cwd can be changed by scripts, which are not foreground processes(? I think). If there is a way of detecting when a bash script is running (or detecting when a script inserts Enter) then that would be more efficient

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the script is run in a sub-process (which is in my understanding equivalent to foreground process) like this

bash script.sh or ./script.sh

it shouldn't be able to change the CWD. But if somebody sources a script which changes the cwd script like this

source script.sh

yes then it could the change the CWD. This is probably really rare so you have to decide if it is worth taking into acount..

Also there is already some kind of precedent set with the remember_position which relies on the Enter key being pressed. Using the contents_changed signal introduces a second mechanism for similar issue: Both want to check if a command was executed. IMO, ideally both should rely on the same mechanism.

@felix-andreas

Copy link
Copy Markdown
Member

Do you know what's the point of current_directory_uri_changed, it sounds like a signal which should get emitted when the directory changes, but it seems to never get emitted and calling get_current_directory_uri returns null on my machine.

Shouldn't it be technically possible to get notified whenever the CWD changes, without relying on continuously polling for it? Similar how get_shell_location

public string get_shell_location () {
int pid = (!) (this.child_pid);
try {
return GLib.FileUtils.read_link ("/proc/%d/cwd".printf (pid));
} catch (GLib.FileError error) {
/* Tab name disambiguation may call this before shell location available. */
/* No terminal warning needed */
return "";
}
}

use the Linux /proc/<pid>/cwd file, should it be possible to set up an watcher using something like inotify_add_watch ? Or maybe trace the chdir syscalls of the child processes?

@jeremypw

Copy link
Copy Markdown
Collaborator Author

@andreasfelix Thanks for the review!
I thought I took out save_open_terminals - I intended to. It doesn't seem necessary when updating the labels. I originally only had the labels update when the user entered Return then changed it to contents_changed so that if a script changed the cwd it was reflected in the labels.

The updating is throttled by a timeout so I do not think it is particularly expensive.

You are right that a better solution would to be notified of a cwd change rather than reacting to signals that might have changed the cwd (would still have to react to adding, removing and switching tabs though). I did not know how to do that but I'll look into your suggestions.

@jeremypw

Copy link
Copy Markdown
Collaborator Author

Isn't /proc/ a virtual directory? So I don't know whether you can monitor it.

@felix-andreas

Copy link
Copy Markdown
Member

oh yes :/ according to stackoverflow seems like watching the proc filesystem is not possible.

* Re-use timeout if running instead of recreating on every call
* Avoid possibility of timeout running continuously
* Do not save terminals when updating labels
* Call update labels max 2 times per sec
@felix-andreas

felix-andreas commented May 24, 2021 •

Copy link
Copy Markdown
Member

IMO, the easiest solution would be to implement check_cwd_changed function in the TerminalWidget, which emits a cwd_changed signal if the CWD has changed.

Then you could register the t.cwd_changed.connect (schedule_name_check); and call current_terminal.check_cwd_changed every time Enter is pressed.

I think this way the code would be a little bit more self documenting, because it makes clear that the names has to be checked every time a terminal widget's cwd changes. And if in the future somebody comes up with some Posix/Linux magic, they just have to replace the logic of the TerminalWidget.cwd_changed signal.

But this way would not catch the case the CWD is changed by a script, but it would be an improvement over how Mate terminal, Gnome terminal or tilix handle it.

@jeremypw

Copy link
Copy Markdown
Collaborator Author

The last commit reduces the cost of calling schedule_name_check rapidly. If two calls arrive within 500mS then the existing timeout just keeps running for another 250+mS. The call simply performs an int comparison and checks a boolean.

I do not see a lot of advantage in creating a new terminal signal tbh instead of monitoring the existing terminal signals.

@felix-andreas

felix-andreas commented May 24, 2021 •

Copy link
Copy Markdown
Member

If you rather want to cover all cases by using contents_changed, I would still move this logic to the TerminalWidget and store the cwd into an instance variable and emit an cwd_changed if the cwd changes. This should scale better with a larger number of tabs because the check_for_tabs_with_same_name only has to run if one of the TerminalWidgets cwd actually changes.

@jeremypw

Copy link
Copy Markdown
Collaborator Author

But then you have each terminal running code everytime its contents changes instead of MainWindow. Each terminal would have to connect to and process the contents_changed signal instead of MainWindow and then emit another signal which MainWindow has to process.

I suppose it might be worth it as check_for_tabs_with_same_name is fairly expensive but does not run very often now.

@felix-andreas

felix-andreas commented May 24, 2021 •

Copy link
Copy Markdown
Member

Isn't the contents_changed a signal of the TerminalWidget? To my understanding it gets emitted for each TerminalWidget individually and each of them should process only if their contents change. Any isn't it exactly the same in the current state of the PR?

@jeremypw

Copy link
Copy Markdown
Collaborator Author

contents_changed is a signal of Vte.Terminal, the TerminalWidget does not do any processing on it currently so would have to connect to maintain a cwd property and decide whether to notify MainWindow.

@felix-andreas

Copy link
Copy Markdown
Member

Yes, this is what I suggested. But if you don't like that, I could do a separate PR afterwards. The PR is probably still worth merging, as it is already a big improvement over the current state.

@jeremypw

Copy link
Copy Markdown
Collaborator Author

It should be possible for MainWindow to maintain a cwd property on each terminal and ignore signals where it hasn't changed but it would mean calls to get_shell_location () and comparison with the current cwd every time the contents changes - this is more expensive (and more code) than calling schedule_name_check.

@felix-andreas

Copy link
Copy Markdown
Member

schedule_name_check calls the get_shell_location for all terminal widgets. so it should be much more expensive than a single call to get_shell_location. at least to my understanding of the code.

@jeremypw

Copy link
Copy Markdown
Collaborator Author

One advantage of your approach is that it could be more responsive - it may not be necessary to throttle updating the labels. I'll give it a try.

@jeremypw

Copy link
Copy Markdown
Collaborator Author

I have to admit your way works at least as well and is more responsive 😄 because I removed the throttle.

The only case it doesn't work (and probably the original didnt) is when you drag a tab out of a window - the remaining tab labels are not updated.

Comment thread src/MainWindow.vala Resolved
Co-authored-by: Felix Andreas <felix.andreas95@googlemail.com>
@jeremypw

Copy link
Copy Markdown
Collaborator Author

@andreasfelix You might like to look at elementary/code#1050 for comparison. This uses a different method of disambiguation based on File functions which is a little simpler but in theory could result in one tab name being much longer than the other whereas my method ensures they are the same length.

Comment thread src/MainWindow.vala Resolved
Comment thread src/Widgets/TerminalWidget.vala Resolved
Comment thread src/Widgets/TerminalWidget.vala Resolved
Comment thread src/MainWindow.vala Resolved
Comment thread src/MainWindow.vala Resolved
Comment thread src/MainWindow.vala Resolved
Comment thread src/MainWindow.vala Resolved
resized = true;
});

contents_changed.connect (check_cwd_changed);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing that still bother me a little bit, is that this is also triggered for foreground processes. Guarding it with a has_foreground_process check probably doesn't make sense, because I assume they are comparatively expensive.

Anyway it probably doesn't matter so much, I didn't see any performance differences on my machine.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One solution would be to unregistered the callback when a foreground process is running and re-register it on child_exited. But this is probably a unnecessary micro optimizations, just came to my mind.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if foreground processes cannot change the cwd then this check is unnecessary. But I doubt whether the extra complexity of code is worth the (undetectable) performance gain.

* Lose unnecessary boolean
* Check names directly on tab restored etc rather than on switch page
internal const string DEFAULT_LABEL = _("Terminal");
public Terminal.Application app;
public string terminal_id;
public string current_working_directory { get; private set; default = "";}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codestyle

Suggested change
public string current_working_directory { get; private set; default = "";}
public string current_working_directory { get; private set; default = ""; }

@felix-andreas felix-andreas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good to me! It solves both linked issues. And even though the callback of the contents_changed signal is called multiple times per second when using the terminal, I can't measure any performance differences.

@jeremypw

Copy link
Copy Markdown
Collaborator Author

Thanks for your patience in collaborating on this! I don't particularly like connecting to contents-changed but at least the handler is now fairly inexpensive.

@jeremypw
jeremypw merged commit c3e36fb into master May 25, 2021
@jeremypw
jeremypw deleted the fix-same-name-regression branch May 25, 2021 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tabs with same name not disambiguated with custom prompt 4-5% CPU usage for idle terminal

2 participants